﻿using System;
using System.IO;
using System.Windows.Markup;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyThirdWPFDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button btn;

        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(string xamlFile)
        {
            //设置窗体
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "动态加载Xaml文件";

            //从外部xaml文件获取内容
            DependencyObject rootElement;
            using(FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }

            this.Content = rootElement;

            btn = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
            btn.Click += btn_Click;
        }

        private void btn_Click(object sender,RoutedEventArgs e)
        {
            btn.Content = "干嘛！";
        }
    }
}
